//
//  MovieViewController.m
//  12.30_homework_豆瓣电影MVC
//
//  Created by dllo on 15/12/30.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieCell.h"
#import "MovieModel.h"
#import "UIImageView+WebCache.h"
#import "MovieThreeViewController.h"
#import "ThreeCollectionViewController.h"
#import "MovieDetailViewController.h"

@interface MovieViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *arrModel;

@property (nonatomic, retain) NSIndexPath *collectionIndex;

@end

@implementation MovieViewController

- (void)dealloc {
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"电影";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"btn_nav_list@2x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:(UIBarButtonItemStylePlain) target:self action:@selector(ThreeMovie:)];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:(UITableViewStylePlain)];
    [self.view addSubview:_tableView];
    [_tableView release];
    
    [self.tableView registerClass:[MovieCell class] forCellReuseIdentifier:@"cell"];
   
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    NSArray *arrData = [NSArray arrayWithContentsOfFile:path];
    
    self.arrModel = [NSMutableArray array];
    for (NSDictionary *dic in arrData) {
        MovieModel *movieModel = [MovieModel movie];
        [movieModel setValuesForKeysWithDictionary:dic];
        [_arrModel addObject:movieModel];
    }
    
    
}
- (void)ThreeMovie:(UIBarButtonItem *)button {
    
//    MovieThreeViewController *movieVC = [[MovieThreeViewController alloc] init];
//    
////    [self presentViewController:movieVC animated:YES completion:^{
////        
////    }];
//
//    [self.navigationController pushViewController:movieVC animated:NO];
    
    ThreeCollectionViewController *three = [[ThreeCollectionViewController alloc] init];
    [self.navigationController pushViewController:three animated:NO];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arrModel.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    self.collectionIndex = indexPath;
    
    MovieCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    MovieModel *movieModel = _arrModel[indexPath.row];
    
    
    cell.title.text = movieModel.title;
    cell.pubdate.text = movieModel.pubdate;
    cell.rating.text = movieModel.rating;
    [cell.picImage sd_setImageWithURL:[NSURL URLWithString:movieModel.image]];

    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MovieDetailViewController *movieDetail = [[MovieDetailViewController alloc] initWithNibName:@"MovieDetailViewController" bundle:[NSBundle mainBundle]];
    
    movieDetail.indexPath = indexPath;
    
    [self.navigationController pushViewController:movieDetail animated:YES];
    
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 220;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
